/*
 * 
 */
package fr.univtln.superplanning.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.univtln.superplanning.api.ISuperplanningManager;
import fr.univtln.superplanning.api.study.Teaching;
import fr.univtln.superplanning.gui.MainWindow;

public class TeachingController implements IGenericController, ListSelectionListener,
		ActionListener {

	private MainWindow mainWindow; // view
	private ISuperplanningManager superplanningManager;
	private Teaching teaching;

	public TeachingController(ISuperplanningManager superplanningManager, MainWindow mainWindow,
			Teaching teaching) {
		this.mainWindow = mainWindow;
		this.superplanningManager = superplanningManager;
		this.teaching = teaching;
		initializeListeners();
	}

	public MainWindow getMainWindow() {
		return mainWindow;
	}

	public void setMainWindow(MainWindow mainWindow) {
		this.mainWindow = mainWindow;
	}

	public ISuperplanningManager getSuperplanningManager() {
		return superplanningManager;
	}

	public void setSuperplanningManager(ISuperplanningManager superplanningManager) {
		this.superplanningManager = superplanningManager;
	}

	public Teaching getTeaching() {
		return teaching;
	}

	public void setTeaching(Teaching teaching) {
		this.teaching = teaching;
	}

	@Override
	public void updateUi() {
		// get all teaching codes
		List<String> teachingCodes = superplanningManager.getStudyManager().getAllTeachingCodes();

		mainWindow.getCoursePanelListcoursePanelList().setListData(teachingCodes.toArray());

		updateCurrentCourse();
	}

	/**
	 * This method reset fields.
	 */
	public void resetUi() {
		mainWindow.getCoursePanelModifyPanelNameTextField().setText("");
	}

	public void updateCurrentCourse() {
		String selectedTeachingCode = (String) mainWindow.getCoursePanelListcoursePanelList()
				.getSelectedValue();

		if (selectedTeachingCode != null) {
			Teaching teaching = superplanningManager.getStudyManager().getTeachingByCode(
					selectedTeachingCode);

			// If the teaching exists, update the view
			if (teaching != null) {
				mainWindow.getCoursePanelModifyPanelNameTextField().setText(teaching.getCode());
			}
		}
	}

	private void initializeListeners() {
		mainWindow.getCoursePanelListcoursePanelList().addListSelectionListener(this);
		mainWindow.getCourseCancelButton().addActionListener(this);
		mainWindow.getCourseSaveButton().addActionListener(this);
		mainWindow.getCourseModifyButton().addActionListener(this);
		mainWindow.getCoursePanelModifyPanelNewButton().addActionListener(this);

	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		// test the source
		if (e.getSource() == mainWindow.getCoursePanelListcoursePanelList()) {
			updateCurrentCourse();
		}
	}

	private void save() {

		// Create a teaching according with the field values
		Teaching teaching = new Teaching(mainWindow.getCoursePanelModifyPanelNameTextField()
				.getText(), mainWindow.getCoursePanelModifyPanelNameTextField().getText(), "");

		// Save the teacher !
		superplanningManager.getStudyManager().insertTeaching(teaching);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == mainWindow.getCourseSaveButton()) {
			save();
			// and update the IU
			updateUi();
		} else if (e.getSource() == mainWindow.getCourseCancelButton()) {

		} else if (e.getSource() == mainWindow.getCourseModifyButton()) {

		} else if (e.getSource() == mainWindow.getCoursePanelModifyPanelNewButton()) {
			resetUi();
		}
	}
}
